#import "SEGMarketView.h"


#import <UIImageView+WebCache.h>
#import "UIImageView+SEGWebCache.h"

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

/**************************SEGMarketView************************/
@implementation SEGMarketView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kMarketViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.mainTitleLabel];
    [self addSubview:self.contentTitleLabel];
    [self addSubview:self.priceLabel];
    
    [self addSubview:self.onePictureImageVIew];

    self.backgroundColor = kColor_L3;
    return self;
}

-(void)setWithLayout:(SEGMarketViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;
    
    if (self.height <= 0) return;

    self.mainTitleLabel.textLayout = layout.mainTitleLabelTextLayout;
    self.contentTitleLabel.textLayout = layout.contentTitleLabelTextLayout;
    self.priceLabel.textLayout = layout.priceLabelTextLayout;
    [self.onePictureImageVIew setImageWithURL:[NSURL URLWithString:_layout.data.image] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];

    [self setupUI];
}

//界面布局
-(void)setupUI{
    //内容文本（标题）
    [self.mainTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).mas_offset(10);
        make.left.equalTo(self).mas_offset(10);
        make.height.mas_equalTo(_layout.mainTitleLabelHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth -_layout.priceLabelWidth - 20);
    }];
    
    
    //价格
    [self.priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).mas_offset(10);
        make.right.equalTo(self).mas_offset(-10);
        make.width.mas_equalTo(self.layout.priceLabelWidth);
        make.height.mas_equalTo(_layout.priceLabelHeight);
    }];
    
    
    //单张图
    [self.onePictureImageVIew mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainTitleLabel.mas_bottom).mas_offset(10);
        make.left.equalTo(self).mas_offset(10);
        make.height.width.mas_equalTo(72.5);
    }];
    
    //描述内容
    [self.contentTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.onePictureImageVIew);
        make.left.equalTo(self.onePictureImageVIew.mas_right).mas_offset(5);
        make.height.mas_equalTo(_layout.contentTitleLabelHeight);
        make.right.equalTo(self).mas_offset(-10);
    }];
    
    self.mainTitleLabel.textAlignment = NSTextAlignmentLeft;
    self.mainTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
    self.contentTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.contentTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
    self.priceLabel.textAlignment = NSTextAlignmentRight;
    self.priceLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
}

#pragma mark - getter
-(SEGLabel *)mainTitleLabel{
    if (!_mainTitleLabel) {
        _mainTitleLabel = [[SEGLabel alloc]init];
        _mainTitleLabel.userInteractionEnabled = NO;
        _mainTitleLabel.textAlignment = NSTextAlignmentCenter;
        _mainTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _mainTitleLabel.displaysAsynchronously = YES;
        _mainTitleLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _mainTitleLabel.fadeOnHighlight = NO;
        _mainTitleLabel.numberOfLines = 0;
        _mainTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _mainTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _mainTitleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        //if (kDebug)_mainTitleLabel.backgroundColor = kSegRandomColor;
    }
    return _mainTitleLabel;
}

-(SEGLabel *)contentTitleLabel{
    if (!_contentTitleLabel) {
        _contentTitleLabel = [[SEGLabel alloc]init];
        _contentTitleLabel.userInteractionEnabled = NO;
        _contentTitleLabel.textAlignment = NSTextAlignmentCenter;
        _contentTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _contentTitleLabel.displaysAsynchronously = YES;
        _contentTitleLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _contentTitleLabel.fadeOnHighlight = NO;
        _contentTitleLabel.numberOfLines = 0;
        _contentTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentTitleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        //if (kDebug)_contentTitleLabel.backgroundColor = kSegRandomColor;
    }
    return _contentTitleLabel;
}

-(SEGLabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[SEGLabel alloc]init];
        _priceLabel.userInteractionEnabled = NO;
        _priceLabel.textAlignment = NSTextAlignmentCenter;
        _priceLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _priceLabel.displaysAsynchronously = YES;
        _priceLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _priceLabel.fadeOnHighlight = NO;
        _priceLabel.numberOfLines = 0;
        _priceLabel.fadeOnAsynchronouslyDisplay = NO;
        _priceLabel.fadeOnAsynchronouslyDisplay = NO;
        _priceLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        //if (kDebug)_priceLabel.backgroundColor = kSegRandomColor;
    }
    return _priceLabel;
}


-(UIImageView *)onePictureImageVIew{
    if (!_onePictureImageVIew) {
        _onePictureImageVIew = [[UIImageView alloc]init];
        _onePictureImageVIew.image = [UIImage imageWithContentsOfFile:SEGPicName(@"")];
        _onePictureImageVIew.contentMode = UIViewContentModeScaleAspectFill;
        _onePictureImageVIew.layer.masksToBounds = YES;
        //_onePictureImageVIew.layer.cornerRadius = 0.5;
        //if (kDebug)_onePictureImageVIew.backgroundColor = kSegRandomColor;
    }
    return _onePictureImageVIew;
}


@end
